<template>
  <div class="app-container">
    <el-row :gutter="6">
      <el-col :xs="24" :sm="24" :lg="6">
        <el-card class="box-card" :style="'height:' + left_height">
          <dept :dict_this="sup_this"/>
        </el-card>
        <el-card class="box-card" :style="'height:' + left_height">
          <job :dict_this="sup_this"/>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="18">
        <el-card class="box-card" :style="'height:' + height">
          <eHeader :query="query" :dict="dict" :sup_this="sup_this"/>
          <!--表格渲染-->
          <el-table v-loading="loading" :data="data" size="small">
            <el-table-column min-width="120" :show-overflow-tooltip=true  prop="workerId" label="工号"/>
            <el-table-column min-width="120" :show-overflow-tooltip=true prop="cartNum" label="卡号"/>
            <el-table-column min-width="120" :show-overflow-tooltip=true prop="workerName" label="姓名"/>
            <el-table-column min-width="120" :show-overflow-tooltip=true prop="idNum" label="身份证"/>
            <el-table-column min-width="120" :show-overflow-tooltip=true prop="telNum" label="电话"/>
            <el-table-column min-width="120" :show-overflow-tooltip=true prop="deptId" label="部门">
              <template slot-scope="scope">
                <div v-for="item in dict.dept" :key="item.id">
                  <span v-if="scope.row.deptId==item.id">{{ item.name }}</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column min-width="120" :show-overflow-tooltip=true prop="jobId" label="职位">
              <template slot-scope="scope">
                <div v-for="item in dict.job" :key="item.id">
                  <span v-if="scope.row.jobId==item.id">{{ item.name }}</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column min-width="120" :show-overflow-tooltip=true prop="registrationTime" label="登记时间">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.registrationTime) }}</span>
              </template>
            </el-table-column>
            <el-table-column min-width="120" :show-overflow-tooltip=true prop="carNum" label="车牌"/>
            <el-table-column min-width="120" :show-overflow-tooltip=true prop="remark1" label="备注1"/>
            <el-table-column min-width="120" :show-overflow-tooltip=true prop="remark2" label="备注2"/>
            <el-table-column min-width="120" :show-overflow-tooltip=true prop="remark3" label="备注3"/>
            <el-table-column min-width="120" :show-overflow-tooltip=true label="操作" width="150px" align="center">
              <template slot-scope="scope">
                <edit :dict="dict" :data="scope.row" :sup_this="sup_this"/>
                <el-popover
                  :ref="scope.row.id"
                  placement="top"
                  width="180">
                  <p>确定删除本条数据吗？</p>
                  <div style="text-align: right; margin: 0">
                    <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
                    <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定
                    </el-button>
                  </div>
                  <el-button slot="reference" type="danger" size="mini">删除</el-button>
                </el-popover>
              </template>
            </el-table-column>
          </el-table>
          <!--分页组件-->
          <el-pagination
            :total="total"
            style="margin-top: 8px;float:right"
            layout="total, prev, pager, next, sizes"
            @size-change="sizeChange"
            @current-change="pageChange"/>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import initData from '@/mixins/initData'
  import {del, getDict} from '@/api/worker'
  import {parseTime} from '@/utils/index'
  import eHeader from './module/header'
  import edit from './module/edit'
  import dept from '@/views/system/dept'
  import job from '@/views/system/job'
  export default {
    components: {eHeader, edit,dept,job},
    mixins: [initData],
    data() {
      return {
        height: document.documentElement.clientHeight - 115 + 'px;',
        left_height: (document.documentElement.clientHeight - 115)/2 + 'px;',
        delLoading: false, sup_this: this,
        dict: {}
      }
    },
    created() {
      this.$nextTick(() => {
        this.init()
        this.getDict()
      })
    },
    methods: {
      parseTime,
      getDict(){
        getDict().then(res => {
          this.dict = res
        }).catch(err => {
          console.log(err.response.data.message)
        })
      },
      beforeInit() {
        this.url = 'api/worker'
        const sort = 'id,desc'
        this.params = {page: this.page, size: this.size, sort: sort}
        const query = this.query
        const cartNum = query.cartNum
        const workerDeviceNum = query.workerDeviceNum
        const workerName = query.workerName
        const idNum = query.idNum
        const deptId = query.deptId
        if (cartNum) {
          this.params['cartNum'] = cartNum
        }
        if (workerDeviceNum) {
          this.params['workerDeviceNum'] = workerDeviceNum
        }
        if (workerName) {
          this.params['workerName'] = workerName
        }
        if (idNum) {
          this.params['idNum'] = idNum
        }
        if (deptId) {
          this.params['deptId'] = deptId
        }
        return true
      },
      subDelete(id) {
        this.delLoading = true
        del(id).then(res => {
          this.delLoading = false
          this.$refs[id].doClose()
          this.init()
          this.$notify({
            title: '删除成功',
            type: 'success',
            duration: 2500
          })
        }).catch(err => {
          this.delLoading = false
          this.$refs[id].doClose()
          console.log(err.response.data.message)
        })
      }
    }
  }
</script>

<style scoped>
</style>
